package com.yyl.video

import android.annotation.SuppressLint
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.os.Handler
import android.os.Message
import android.support.v4.os.EnvironmentCompat
import android.util.Log
import android.widget.SeekBar
import com.yyl.jni.MyVideo
import com.yyl.jni.player.YPlayer
import kotlinx.android.synthetic.main.activity_main.*
import com.danikula.videocache.HttpProxyCacheServer
import com.yyl.jni.utils.TimeUtils
import java.io.File


class MainActivity : AppCompatActivity() {
        private val path = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
//    private val path = "http://haobashi.me/20180525T003000_0_10341127_0.ts"

//    private val path = "http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8"
//    private val path = "https://www.wowza.com/downloads/images/sample.mp4"
//public static final String path = "rtsp://video.fjtu.com.cn/vs01/flws/flws_01.rm";

    //    private val path = "http://mpge.5nd.com/2015/2015-11-26/69708/1.mp3"
//    private val path = "1233"
    val player = YPlayer()
    val recordFile = File(Environment.getExternalStorageDirectory(), "test.aac")
    private val proxy by lazy { HttpProxyCacheServer(this) }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val proxyUrl = proxy.getProxyUrl(path)
//        val proxyUrl = path

        videoTime.text = MyVideo().version
        //  MyVideo().setFFmpeg()
        player.setSurfaceView(videoView)
        prepared.setOnClickListener {
            player.setSource(proxyUrl)
            player.prepared()
        }

        play.setOnClickListener {
            player.playChange(true)
        }
        pause.setOnClickListener {
            player.playChange(false)
        }
        stop.setOnClickListener {
            player.onStop()
        }

        volume.setOnClickListener {
            player.setVolume(audioValue.text.toString().toIntOrNull() ?: 1)
        }
        speed.setOnClickListener {
            player.setSpeed(audioValue.text.toString().toDouble())
        }
        mute.setOnClickListener {
            player.setMute(audioValue.text.toString().toIntOrNull() ?: 1)
        }
        pitch.setOnClickListener {
            player.setPitch(audioValue.text.toString().toDouble())
        }
        record.setOnClickListener {
            recordFile.delete()
            player.startRecord(recordFile)
        }
        recordstop.setOnClickListener {
            player.stopRecord()
        }
        recordResume.setOnClickListener {
            player.resumeRcord()
        }
        recordPause.setOnClickListener {
            player.pauseRecord()
        }





        player.setOnEventListener { event, arg1, arg2 ->
            when (event) {
                0 -> {
                    if (arg1 == 1) player.start()
                    Log.i("yyl", "准备开始  $event   arg1= $arg1   arg2= $arg2")
                }
                1 -> {//#define EVENT_STATE_PLAY 1
                    //  Log.i("yyl", "setOnEventListener  $event   arg1= $arg1   arg2= $arg2")
                }
                2 -> {//EVENT_STATE_TIME 2
                    //   Log.i("yyl", "setOnEventListener  $event   arg1= $arg1   arg2= $arg2")
                    handler.sendEmptyMessage(0)
                }
                3 -> {//EVENT_STATE_COMPLETE 3
                    Log.i("yyl", "播放完成  $event   arg1= $arg1   arg2= $arg2")
                    player.onStop()
                }
                4 -> {//EVENT_STATE_PCM_DB  pcm 分贝值

                }
                5 -> {//EVENT_STATE_PCM_BUFFER

                }
                10 -> {//EVENT_STATE_ERROR
                    Log.i("yyl", "出错了  ERROR  $event   arg1= $arg1   arg2= $arg2")
                    player.onStop()
                }
                else -> {
                    Log.i("yyl", "setOnEventListener  $event   arg1= $arg1   arg2= $arg2")
                }


            }
        }


        seekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                position = progress * player.duration / 1000
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
                seekTouch = true
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
                player.seekTo(position)
                seekTouch = false
            }

        })
    }

    private val handler = @SuppressLint("HandlerLeak")
    object : Handler() {
        override fun handleMessage(msg: Message?) {
            videoTime.text = TimeUtils.secdsToDateFormat(player.currentTime.toLong()) + " / " + TimeUtils.secdsToDateFormat(player.duration)
            if (!seekTouch && player.duration > 0) {
                //  Log.i("yyl", "currentTime= ${player.currentTime}   duration= ${player.duration}")
                seekbar.progress = (player.currentTime / (player.duration) * 1000).toInt()
            }
        }
    }
    var position = 0L
    var seekTouch = false


}
